import { logger } from '../core/logger';
import { areaInfo } from '../models/areaInfo';
import { clientBase, HOST } from './clientBase';
import { ajaxResult } from '../models/ajaxResult';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class areaClient extends clientBase {
  create(data: areaInfo): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/areas/create`, data, this.withToken)
        .then((response: AxiosResponse) => {
          if (response.status == 200) {
            let ret: string = response.data.result;
            resolve(this.buildSuccessResult<string>(ret));
          } else
            resolve(this.buildFailedResult<string>(response.statusText));
        }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  update(data: areaInfo): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/areas/update`, data, this.withToken)
        .then((response: AxiosResponse) => {
          if (response.status == 200) {
            let ret: string = response.data.result;
            resolve(this.buildSuccessResult<string>(ret));
          } else
            resolve(this.buildFailedResult<string>(response.statusText));
        }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  delete(areaId: string): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.delete<string, AxiosResponse<string>, any>(`${HOST}api/areas/del?areaId=${areaId}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  getPages(pageIndex: number, keywords: string): Promise<ajaxResult<Array<areaInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/areas/getPaging?pageIndex=${pageIndex}&keywords=${keywords}`, this.withToken)
        .then((response: AxiosResponse) => {
          if (response.status == 200) {
            let ret: Array<areaInfo> = response.data.result?.data ?? [];
            resolve(this.buildSuccessResult<Array<areaInfo>>(ret));
          } else
            resolve(this.buildFailedResult<Array<areaInfo>>(response.statusText));
        }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<areaInfo>>(err));
      });
    });
  }

  getAll(): Promise<ajaxResult<Array<areaInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/areas/getAll`)
        .then((response: AxiosResponse) => {
          if (response.status == 200) {
            let ret: Array<areaInfo> = response.data.result;
            resolve(this.buildSuccessResult<Array<areaInfo>>(ret));
          } else
            resolve(this.buildFailedResult<Array<areaInfo>>(response.statusText));
        }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<areaInfo>>(err));
      });
    });
  }
}
export default new areaClient();